client-python
kubernetes-incubator/client-python是针对Kubernetes使用python开发的client,可以很方便地让用户通过python访问Kubernetes集群。这次分析将选择典型场景介绍client-python是如何使用的。
要注意的是,例子中的代码需要在使用kubeconfig机制的环境中才能运行成功。
创建
从yaml文件中创建:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import yaml from os import path from kubernetes import client,config from kubernetes.client.rest import ApiException config.load_kube_config() api_instance = client.CoreV1Api() f = open(path.join(path.dirname(__file__), "ubuntu-svc.yaml")) body = yaml.load(f) try: api_response = api_instance.create_namespaced_service(namespace=body['metadata']['namespace'], body=body) except ApiException as ex: print ex
|
从dict创建:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from kubernetes import client,config from kubernetes.client.rest import ApiException config.load_kube_config() api_instance = client.CoreV1Api() secret_metadata = {} secret_metadata['name'] = "abcd" secret_metadata['namespace'] = "default" secret_data = {"key": "MTIzNDU2Cg=="} secret_body = client.V1Secret(api_version="v1", kind="Secret", metadata=secret_metadata, data=secret_data, type="kubernetes.io/rbd") try: api_response = api_instance.create_namespaced_secret("default", secret_body) print api_response except ApiException as ex: print ex
|
获取资源列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from kubernetes import client,config from kubernetes.client.rest import ApiException config.load_kube_config() api_instance = client.CoreV1Api() namespace = "default" label_selector = "app=ubuntu" field_selector = "" try: api_response = api_instance.list_namespaced_service(namespace=namespace, label_selector=label_selector, field_selector=field_selector, pretty=True, watch=False).to_dict() print api_response except ApiException as ex: print ex
|
获取资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| from kubernetes import client,config from kubernetes.client.rest import ApiException config.load_kube_config() api_instance = client.CoreV1Api() namespace = "default" name = "ubuntu11" try: api_response = api_instance.read_namespaced_service(name=name, namespace=namespace, pretty=True, exact=True).to_dict() print api_response except ApiException as ex: print ex
|
更新资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| from kubernetes import client,config from kubernetes.client.rest import ApiException config.load_kube_config() api_instance = client.CoreV1Api() namespace = "default" name = "ubuntu11" try: body = api_instance.read_namespaced_service(name=name, namespace=namespace, pretty=True, exact=True).to_dict() body['metadata']['labels']['type'] = "service" api_response = api_instance.patch_namespaced_service(name=body['metadata']['name'], namespace=body['metadata']['namespace'], body=body) except ApiException as ex: print ex
|
删除资源
1 2 3 4 5 6 7 8 9 10 11 12 13
| from kubernetes import client,config from kubernetes.client.rest import ApiException config.load_kube_config() api_instance = client.CoreV1Api() namespace = "default" name = "ubuntu11" try: api_response = api_instance.delete_namespaced_service(namespace=namespace, name=name) except ApiException as ex: print ex
|
附录
service yaml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apiVersion: v1 kind: Service metadata: name: ubuntu11 namespace: default labels: app: ubuntu spec: ports: - port: 22 targetPort: 22 name: ssh selector: app: ubuntu
|